package com.ycMc.service.impl;

import com.ycMc.bean.request.MoneyRecordRequest;
import com.ycMc.common.bean.PageBean;
import com.ycMc.domain.MoneyRecord;
import com.ycMc.domain.User;
import com.ycMc.mapper.MoneyRecordMapper;
import com.ycMc.mapper.UserMapper;
import com.ycMc.service.BaseService;
import com.ycMc.service.MoneyRecordService;
import com.ycMc.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by libo2 on 2017/8/23.
 */
@Service
public class MoneyRecordServiceImpl extends BaseService implements MoneyRecordService {


    @Autowired
    private MoneyRecordMapper moneyRecordMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public void delete(String moneyRecordId) {
        moneyRecordMapper.delete(moneyRecordId);
    }

    @Override
    public List<MoneyRecord> select(MoneyRecordRequest request) {
        final int offset = PageBean.countOffset(request.getPageSize(),request.getCurrentPage());
        final int _currentPage = PageBean.countCurrentPage(request.getCurrentPage());
        return moneyRecordMapper.select(request.getUserId(),request.getTradeType(),request.getOperatorId(),request.getPayMethod(),request.getPayId(),request.getOrderId(),request.getMoneyRecordId(),offset,request.getPageSize());
    }

    @Override
    public List<MoneyRecord> selectAll() {
        return moneyRecordMapper.select(null,null,null,null,null,null,null,0,0);
    }

    @Override
    @Transactional
    public void insert(MoneyRecord moneyRecord, String operatorId) {
        moneyRecord.setMoneyRecordId(BaseUtils.generateMoneyRecordId());
        moneyRecord.setId(0);
        moneyRecord.setCreateTime(System.currentTimeMillis());
        moneyRecord.setOperatorId(operatorId);
        if(moneyRecord.getTradeType().equals("进账")){
            User user = new User();
            User tempUser = userMapper.selectOne(moneyRecord.getUserId());
            user.setUserId(moneyRecord.getUserId());
            user.setUserMoney(tempUser.getUserMoney()+moneyRecord.getMoney());
            userMapper.updateUserMoney(user);
        }else if (moneyRecord.getTradeType().equals("出账")){
            User user = new User();
            User tempUser = userMapper.selectOne(moneyRecord.getUserId());
            user.setUserId(moneyRecord.getUserId());
            user.setUserMoney(tempUser.getUserMoney()-moneyRecord.getMoney());
            userMapper.updateUserMoney(user);
        }
        moneyRecordMapper.insert(moneyRecord);
    }

    @Override
    public List<MoneyRecord> selectByUserId(String userId) {

        return         moneyRecordMapper.select(userId,null,null,null,null,null,null,0,0);
    }

    @Override
    public MoneyRecord selectOne(String moneyRecordId) {
        return moneyRecordMapper.selectOne(moneyRecordId);
    }

    @Override
    public void update(MoneyRecord moneyRecord) {
        moneyRecordMapper.update(moneyRecord);
    }


    @Override
    public PageBean query(MoneyRecordRequest request) {
        final int offset = PageBean.countOffset(request.getPageSize(),request.getCurrentPage());
        final int _currentPage = PageBean.countCurrentPage(request.getCurrentPage());
        List<MoneyRecord> moneyRecords = moneyRecordMapper.query(request.getKeyword(),request.getSelectedUserId(),request.getTradeType(),request.getPayMethod(),offset,request.getPageSize(),request.getStartTime(),request.getEndTime());
        return createPageBean(_currentPage,moneyRecords,offset);
    }
}
